/*
 * Copyright (C) 2013 Andreas Stuetz <andreas.stuetz@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package br.liveo.ndrawer.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import br.liveo.ndrawer.R;
import br.liveo.ndrawer.listener.ScrollListener;
import br.liveo.ndrawer.ui.activity.MainActivity;
import egame.libs.android.util.DebugUtils;

public class SuperAwesomeCardFragment extends Fragment {

	private MainActivity mainActivity;
	private static final String ARG_POSITION = "position";

	private int position;

	private ActionBar actionBar;

	private int heightItem;

	private ScrollListener mScrollListener;

	View rootView;
	GridView v;

	public static SuperAwesomeCardFragment newInstance(MainActivity activity,
			int position, ScrollListener scrollListener) {
		SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
		f.mainActivity = activity;
		f.mScrollListener = scrollListener;
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		position = getArguments().getInt(ARG_POSITION);
		actionBar = mainActivity.getSupportActionBar();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		rootView = inflater.inflate(R.layout.view_layout, container, false);

		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		v = (GridView) rootView.findViewById(R.id.list);

		String[] values = new String[] { "Android List View",
				"Adapter implementation", "Simple List View In Android",
				"Create List View Android", "Android Example",
				"List View Source Code", "List View Array Adapter",
				"Android Example List View", "Android List View",
				"Adapter implementation", "Simple List View In Android",
				"Create List View Android", "Android Example",
				"List View Source Code", "List View Array Adapter",
				"Android Example List View", "Android List View",
				"Adapter implementation", "Simple List View In Android",
				"Create List View Android", "Android Example",
				"List View Source Code", "List View Array Adapter",
				"Android Example List View", "Android List View",
				"Adapter implementation", "Simple List View In Android",
				"Create List View Android", "Android Example",
				"List View Source Code", "List View Array Adapter",
				"Android Example List View", "Android List View",
				"Adapter implementation", "Simple List View In Android",
				"Create List View Android", "Android Example",
				"List View Source Code", "List View Array Adapter",
				"Android Example List View", "Android List View",
				"Adapter implementation", "Simple List View In Android",
				"Create List View Android", "Android Example",
				"List View Source Code", "List View Array Adapter",
				"Android Example List View" };

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, android.R.id.text1, values);

		// Assign adapter to ListView
		v.setAdapter(adapter);

		v.setOnScrollListener(new OnScrollListener() {
			int mLastFirstVisibleItem = 0;
			int lastScroll;

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (view.getId() == v.getId()) {
					if (v == null || v.getChildCount() == 0)
						return;

					if (heightItem == 0) {
						heightItem = v.getChildAt(0).getMeasuredHeight();

					}
					final int scroll = v.getFirstVisiblePosition() / 2
							* heightItem + Math.abs(v.getChildAt(0).getTop());

					if (scroll - lastScroll > 150) {
						mScrollListener.onHide();
						lastScroll = scroll;
					}

					if (lastScroll - scroll > 150) {
						mScrollListener.onShow();
						lastScroll = scroll;
					}
					mScrollListener.onTranslate(scroll);
					//
					// DebugUtils
					// .d("SuperAwesomeCardFragment.onActivityCreated(...).new OnScrollListener() {...}",
					// "onScroll:   Kai => " + heightItem +"  " +
					// v.getChildAt(0).getTop());

					// mScrollListener.onTranslate(scroll);

					// final int currentFirstVisibleItem = v
					// .getFirstVisiblePosition();

					// if (mLastFirstVisibleItem < firstVisibleItem) {
					// mScrollListener.onHide();
					// // if (actionBar.isShowing()) {
					// // // actionBar.hide();
					// // }
					// mLastFirstVisibleItem = currentFirstVisibleItem;
					// }
					//
					// if (mLastFirstVisibleItem > firstVisibleItem) {
					// mScrollListener.onShow();
					// // if (!actionBar.isShowing()) {
					// // actionBar.show();
					// // }
					// mLastFirstVisibleItem = currentFirstVisibleItem;
					// }

				}
			}
		});

	}
}